package com.atXiao.food.service.impl;

import com.atXiao.food.config.Log;
import com.atXiao.food.constant.BusSignConstant;
import com.atXiao.food.constant.BuscodeConstant;
import com.atXiao.food.service.OssService;
import com.atXiao.food.util.FileNameGeneratorUtil;
import com.atXiao.food.util.OSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

@Service
public class OssServiceImpl implements OssService {

    public final static Log logger = new Log(OssServiceImpl.class);

    /**
     * 图片存放的文件夹
     */
    @Value("${ali.imageDirectory}")
    private String imageDirectory;

    /**
     * 视频存放的文件夹
     */
    @Value("${ali.videoDirectory}")
    private String videoDirectory;


    @Autowired
    OSSUtil ossUtil;

    @Override
    public String uploadFile(String fileType, MultipartFile file) throws IOException {
        String fileUrl = "";
        InputStream inputStream = file.getInputStream();
        // 原始文件名
        String originalFilename = file.getOriginalFilename();
        logger.info("", BusSignConstant.TASTE_FOOD, BuscodeConstant.FILE_UPLOAD,"原始文件名:"+originalFilename);
        // 生成的新文件名
        String newFileName = FileNameGeneratorUtil.generateFileName(originalFilename);
        logger.info("", BusSignConstant.TASTE_FOOD, BuscodeConstant.FILE_UPLOAD,"新文件名:"+newFileName);
        // 公网可访问的文件名
        // 图片上传
        if ("1".equals(fileType)) {
            fileUrl = ossUtil.uploadInputStreamToOSS(imageDirectory + newFileName, inputStream);
        } else {
            // 视频上传
            fileUrl = ossUtil.uploadInputStreamToOSS(videoDirectory + newFileName, inputStream);

        }
        return fileUrl;
    }
}
